﻿using ETWJ.DTO;
using ETWJ.Iservice;
using ETWJ.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ETWJ.Service
{
    public class ProductService : CommonService<Product, ProductDTO, MyDbContext>, IProductService<Product, ProductDTO>
    {
        public async Task<int> UpdateAsync(ProductDTO dto)
        {
            using (MyDbContext ctx = new MyDbContext())
            {
                BaseService<Product> bs = new BaseService<Product>(ctx);
                var data = await ctx.Set<Product>().Where(e => e.IsDeleted == 0 && e.Id == dto.Id).SingleOrDefaultAsync();
                //var data =await bs.GetByIdAsync(dto.Id);
                data.Name = dto.Name;
                data.Price = dto.Price;
                data.ProductCode = dto.ProductCode;
                data.Type = dto.Type;
                ctx.Entry(data).State = EntityState.Modified;
                return await ctx.SaveChangesAsync();
            }
        }
    }
}
